<?php
namespace App\Http\Controllers\System\Permission;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\TreeResource;
use App\Http\Resources\System\Permission\TreeOrg\OrgResource;
use App\Services\System\Permission\TreeOrgService;
use App\Models\Admin\Auth\Permission\Tree;

class TreeOrgController extends Controller
{

    /**
     * 获取页面列表
     */
    public function getOrgList(Request $request)
    {
        $this->validate($request, [
            'tree_id' => 'required|integer|min:0'
        ]);
        $treeId = $request->get('tree_id');

        $orgs = TreeOrgService::create()->getOrgs($treeId);

        return new TreeResource($orgs);
    }

    /**
     * 获取授权页面
     */
    public function getOrgs(Request $request)
    {
        $this->validate($request, [
            'tree_id' => 'required|integer|min:1'
        ]);
        $treeId = $request->get('tree_id');

        $orgs = Tree::findOrFail($treeId)->orgs;

        return new OrgResource($orgs);
    }

    /**
     * 更新授权页面
     *
     * @param int $treeId
     */
    public function update(Request $request)
    {
        $this->validate($request, [
            'tree_id' => 'required|integer|min:1'
        ]);
        $treeId = $request->input('tree_id');

        $orgs = TreeOrgService::create()->update($treeId, $request);

        return new OrgResource($orgs);
    }
}
